package com.xuhui.xuaiagent.controller;

import com.xuhui.xuaiagent.agent.XuMans;
import com.xuhui.xuaiagent.app.CodeApp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;

/**
 * @author 21829
 */
@RestController
@RequestMapping("/ai")
@Slf4j
public class AiController {


    @Resource
    private CodeApp codeApp;

    @Resource
    private ToolCallback[] allToos;

    @Resource
    private ChatModel dashscopeChatModel;

    private String userMessage;

    /**
     * 同步响应接口
     *
     * @param message 用户提示词
     * @param chatId  对话id
     * @return ai响应对话
     */
    @GetMapping("/love_app/chat/sync")
    private String doChatWithLoveAppSync(String message, String chatId) {
        return codeApp.doChat(message, chatId);
    }

    /**
     * 流式输出响应接口 第一种方式
     *
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/love_app/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> doChatWithLoveAppSSE(String message, String chatId) {
        log.info("接收到ai恋爱大师请求");
        return codeApp.doChatByStream(message, chatId);
    }

//    /**
//     * 流式响应输出接口 第二种方式
//     *
//     * @param message
//     * @param chatId
//     * @return
//     */
//    @GetMapping(value = "/love_app/chat/sse")
//    public Flux<ServerSentEvent<String>> doChatWithLoveAppServerSentEvent(String message, String chatId) {
//        return loveApp.doChatByStream(message, chatId)
//                .map(chunk -> ServerSentEvent.<String>builder()
//                        .data(chunk)
//                        .build());
//    }

    /**
     * 流式输出响应接口 弟三种方式
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/love_app/chat/sse/emitter")
    public SseEmitter doChatWithLoveAppSseEmitter(String message, String chatId) {
        // 创建一个超时时间较长的SseEmitter
        SseEmitter sseEmitter = new SseEmitter(180000L);
        // 获取 Flux 数据流并直接订阅
        codeApp.doChatByStream(message, chatId)
                .subscribe(
                        // 处理每条消息
                        chunk -> {
                            try {
                                sseEmitter.send(chunk);
                            }catch (IOException e){
                                sseEmitter.completeWithError(e);
                            }
                        },
                        // 处理错误
                        sseEmitter::completeWithError,
                        // 处理完成
                        sseEmitter::complete
                );
        // 返回emitter
        return sseEmitter;
    }

    /**
     * 流式调用menus智能体
     *
     * @param message 用户输入提示词
     * @return 智能体响应结果
     */
    @GetMapping("/manus/chat")
    public SseEmitter doChatWithMenus(String message){
        log.info("接收到ai超级智能体请求");
        XuMans xuMans = new XuMans(allToos,dashscopeChatModel);
        SseEmitter sseEmitter = xuMans.runStream(userMessage == message ? "terminate" : message);
        userMessage = message;
        return sseEmitter;
    }
//    @GetMapping("/manus/chat")
//    public String doChatWithMenus(String message){
//        XuMans xuMans = new XuMans(allToos,dashscopeChatModel);
////        return xuMans.runStream(message);
//        return xuMans.run(message);
//    }

}
